home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / hdf / unix / examples.lha / examples / sds / multi_test / multi_test.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-08  |  5.6 KB  |  263 lines

  1. /*
  2.  *                  multi_sds_test.c
  3.  *    A simple routine that tests the basic HDF scientific
  4.  *    data set read/write functions.
  5.  *      
  6.  *      modified to test storage/retrieval of multiple data sets
  7.  *      in same file.
  8.  *
  9.  *      Should be run in a cmdtool or other scrollable window as
  10.  *      output text can be long.
  11.  *
  12.  *       compile with          cc -DSUN -o outfile infile.c
  13.  *
  14.  *       Input file:  multi_test.raw: a binary file containing a 10x10
  15.  *                    array of floating point values, with 1's along
  16.  *                    the diagonal and 0's elsewhere.
  17.  *       Output file: named by user in response to a prompt.
  18.  *
  19.  *       (This program was donated by Paul Girardi and Phil Frulla of 
  20.  *       Schlumberger.)
  21.  */
  22.  
  23. #include "df.h"
  24. #include <stdio.h>
  25. #include <errno.h>
  26.  
  27. FILE *stream;
  28.  
  29. main()
  30. {
  31.  
  32.  
  33.     int    ret, i, j;
  34.     int    rank=2, inRank;
  35.     int32    dimsize[2], inDimsize[2];
  36.     char    fname[50],command[80];
  37.     float    *data_array;
  38.             
  39.     fprintf(stdout,"\n\nWe will read 'multi_test.dat'...\n");
  40.     fprintf(stdout,"  This is a file of 100 floating point values\n");
  41.     fprintf(stdout,"  As a 10x10 matrix: data[x][y]=1.0 where x=y.\n");
  42.     fprintf(stdout,"  We will create an HDF file from it\n");
  43.     fprintf(stdout,"  We will then modify the array 2 times and\n");
  44.     fprintf(stdout,"  write 2 more SDSs to the HDF file.\n");
  45.  
  46.     
  47.     fprintf(stdout,"\nEnter the name of the output HDF file: ");
  48.     fscanf(stdin,"%s",fname);
  49.     
  50.     data_array = (float *)malloc(100*sizeof(float));
  51.  
  52.     fprintf(stdout,"...reading\n");    
  53.     stream = fopen("multi_test.dat","r");
  54.     fread(data_array,sizeof(float),100,stream);
  55.     fclose(stream);
  56.     
  57.     fprintf(stdout,"\n\n'multi_test.dat' has been read...\n\n");
  58.     for(i=0;i<10;i++)
  59.     {
  60.        for(j=0;j<10;j++)
  61.        {
  62.           fprintf(stdout,"%5.2f ",*(data_array+i*10+j));
  63.        }
  64.        fprintf(stdout,"\n");
  65.     }
  66.     fprintf(stdout,"\n\n");
  67.     
  68.     fprintf(stdout,"Attempting to write HDF SDS file...\n");
  69.  
  70.     dimsize[0]= dimsize[1] = 10;
  71.     ret =DFSDsetdims(rank,dimsize);
  72.     ret =DFSDputdata(fname,2,dimsize,data_array);
  73.  
  74.     if (!ret) 
  75.     {
  76.        fprintf(stdout,"...first matrix has been successfully written..\n");
  77.     }
  78.     else
  79.     {
  80.        fprintf(stdout,"\nDFSputdata returned %d\n",ret);
  81.        exit(1);
  82.     }
  83.  
  84. /*
  85.  *
  86.  *  replace '1s' in matrix with '2s' and write second Data Set
  87.  *
  88.  */
  89.  
  90.     for(i=0;i<10;i++)
  91.       {
  92.         *(data_array + 10*i + i) = 2.0;
  93.       }
  94.     ret =DFSDsetdims(rank,dimsize);
  95.     ret =DFSDadddata(fname,2,dimsize,data_array);
  96.  
  97.     if (!ret) 
  98.     {
  99.        fprintf(stdout,"\t...second matrix successfully written...\n");
  100.     }
  101.     else
  102.     {
  103.        fprintf(stdout,"\nDFSDadddata returned %d\n",ret);
  104.        exit(1);
  105.     }
  106. /*
  107.  *
  108.  *  replace '2s' in matrix with '3s' and write third Data Set
  109.  *
  110.  */
  111.     for(i=0;i<10;i++)
  112.       {
  113.         *(data_array + 10*i + i) = 3.0;
  114.       }
  115.     ret =DFSDsetdims(rank,dimsize);
  116.     ret =DFSDadddata(fname,2,dimsize,data_array);
  117.  
  118.     if (!ret) 
  119.     {
  120.        fprintf(stdout,"\t\t...third matrix successfully written...\n\n");
  121.        fprintf(stdout," HDF file successfully written with 3 SDSs.\n\n");
  122.     }
  123.     else
  124.     {
  125.        fprintf(stdout,"\nDFSDadddata returned %d\n",ret);
  126.        exit(1);
  127.     }
  128.  
  129. /*
  130.  *
  131.  *   Use hdfls command line utility to show that HDF file contains 3 SDS
  132.  *
  133.  */
  134.     fprintf(stdout,"Here is a listing of the HDF file contents.\n\n");
  135.     strcpy(command,"hdfls -o ");
  136.     strcat(command,fname);
  137.     system(command);
  138.     fprintf(stdout,"\n\n");
  139.     strcpy(command,"hdfls -l ");
  140.     strcat(command,fname);
  141.     system(command);
  142.  
  143.     fprintf(stdout,"After zeroing the data array we will attempt to \n");
  144.     fprintf(stdout,"read in the newly created HDF file and determine\n");
  145.     fprintf(stdout,"that it contains the correct data\n");
  146.     
  147.     for (i=0;i<100;i++)
  148.     {
  149.        *(data_array + i) = 0.0;
  150.     }
  151.     fprintf(stdout,"\n\n Data initialized, now reading HDF file...\n");
  152.     
  153.     ret = DFSDgetdims(fname,&inRank,inDimsize,2);
  154.     fprintf(stdout,"Data in file has following parameters:\n");
  155.     fprintf(stdout,"     Rank: %d\n",inRank);
  156.     fprintf(stdout,"    Xdims: %d\n",inDimsize[0]);
  157.     fprintf(stdout,"    Ydims: %d\n\n",inDimsize[1]);
  158.  
  159.     ret = DFSDgetdata(fname,2,inDimsize,data_array);
  160.     if (!ret)
  161.     {
  162.         fprintf(stdout,"...first data array read:\n");
  163.     }
  164.     else
  165.     {
  166.         fprintf(stdout,"\nDFSDgetdata returned %d\n",ret);
  167.         exit(1);
  168.     }
  169.  
  170.     for(i=0;i<10;i++)
  171.     {
  172.        for(j=0;j<10;j++)
  173.        {
  174.           fprintf(stdout,"%5.2f ",*(data_array+i*10+j));
  175.        }
  176.        fprintf(stdout,"\n");
  177.     }
  178.  
  179. /*
  180.  *
  181.  *  Re-initialize the data array and get second SDS from file
  182.  *
  183.  */
  184.     for (i=0;i<100;i++)
  185.     {
  186.        *(data_array + i) = 0.0;
  187.     }
  188.     ret = DFSDgetdata(fname,2,inDimsize,data_array);
  189.     if (!ret)
  190.     {
  191.         fprintf(stdout,"...second data array read:\n");
  192.     }
  193.     else
  194.     {
  195.         fprintf(stdout,"\nDFSDgetdata returned %d\n",ret);
  196.         exit(1);
  197.     }
  198.  
  199.     for(i=0;i<10;i++)
  200.     {
  201.        for(j=0;j<10;j++)
  202.        {
  203.           fprintf(stdout,"%5.2f ",*(data_array+i*10+j));
  204.        }
  205.        fprintf(stdout,"\n");
  206.     }
  207.  
  208. /*
  209.  *
  210.  *  Re-initialize the array and get the third SDS from file
  211.  *
  212.  */
  213.     for (i=0;i<100;i++)
  214.     {
  215.        *(data_array + i) = 0.0;
  216.     }
  217.     ret = DFSDgetdata(fname,2,inDimsize,data_array);
  218.     if (!ret)
  219.     {
  220.         fprintf(stdout,"...third data array read:\n");
  221.     }
  222.     else
  223.     {
  224.         fprintf(stdout,"\nDFSDgetdata returned %d\n",ret);
  225.         exit(1);
  226.     }
  227.  
  228.     for(i=0;i<10;i++)
  229.     {
  230.        for(j=0;j<10;j++)
  231.        {
  232.           fprintf(stdout,"%5.2f ",*(data_array+i*10+j));
  233.        }
  234.        fprintf(stdout,"\n");
  235.     }
  236. /*
  237.  *
  238.  *  Try to read a fourth SDS from the file...this should fail !!
  239.  *
  240.  */
  241.     ret = DFSDgetdata(fname,2,inDimsize,data_array);
  242.     if (!ret)
  243.     {
  244.         fprintf(stdout,"...fourth data array read:\n");
  245.         fprintf(stdout,"THIS SHOULD NOT HAVE HAPPENED !!!!\n\n");
  246.             exit(1);
  247.     }
  248.     else
  249.     {
  250.         fprintf(stdout,"\nDFSDgetdata returned %d\n",ret);
  251.             fprintf(stdout,"This error is normal, EOF reached.\n\n");
  252.     }
  253.  
  254.     
  255.     fprintf(stdout,"Test of SDS data file read/write successful !\n");
  256. }
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.